<template>
  <el-dialog
  :title="this.form.id?'编辑目录':'新增目录'"
  :visible="dialogVisible"
  width="400px"
  :before-close="handleClose">
<el-form ref="form" :model="form" :rules="rules" label-width="80px">
  <el-form-item label="所属学科" prop="subjectID">
    <el-select v-model="form.subjectID" placeholder="请选择" class="input">
      <el-option v-for="item in subjectList" :key="item.value" :label="item.label" :value="item.value"></el-option>
    </el-select>
  </el-form-item>
  <el-form-item label="目录名称" prop="directoryName">
    <el-input v-model="form.directoryName" placeholder="请输入目录名称" class="input"></el-input>
  </el-form-item>
</el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="handleClose">取 消</el-button>
    <el-button type="primary" @click="handleadd">确 定</el-button>
  </span>
</el-dialog>
</template>

<script>
import { simple } from '@/api/hmmm/subjects.js'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    directoryCont: {
      type: Object
    }
  },
  data () {
    return {
      form: {
        subjectID: '',
        directoryName: ''
      },
      subjectList: [],
      rules: {
        subjectID: [{ required: true, message: '所属学科不能为空', trigger: 'change' }],
        directoryName: [{ required: true, message: '目录名称不能为空', trigger: 'blur' }]
      }
    }
  },
  created () {
    this.getSimpleSubject()
    this.form = this.directoryCont
  },
  methods: {
    async getSimpleSubject () {
      const { data } = await simple()
      this.subjectList = data
    },
    handleClose () {
      this.$nextTick(() => {
        this.$refs.form.resetFields()
      })
      this.$emit('close')
    },
    handleadd () {
      this.$refs.form.validate(isOk => {
        if (isOk) {
          if (this.form.id) {
            this.$emit('edit', this.form)
          } else {
            this.$emit('add', this.form)
            this.handleClose()
          }
        }
      })
    }
  }
}
</script>

<style scoped lang='scss'>
.el-dialog{
  .el-dialog__body{
   .input{
     width: 280px;
   }
  }
}
</style>
